package org.iabako.client.ui.page;

import com.google.gwt.core.client.GWT;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiTemplate;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.HTMLPanel;
import org.iabako.client.config.page.PageConfigSearchEnum;
import org.iabako.client.config.page.PageEnum;
import org.iabako.client.ui.layout.template.Page;
import org.iabako.client.ui.layout.widget.datatable.DataTable;
import org.iabako.shared.iservice.generic.CRUDServiceAsync;
import org.iabako.shared.iservice.generic.SearchServiceAsync;
import org.iabako.shared.iservice.generic.SuggestServiceAsync;
import org.iabako.shared.querydefinition.dto.QueryDefinitionDTO;

/**
 * Created by jose on 23/03/14.
 */
public abstract class AbstractSearchPage extends Composite {

    @UiTemplate("org.iabako.client.ui.layout.template.Page.ui.xml")
    interface PageTemplate extends UiBinder<HTMLPanel, Page> {
    }

    protected Page page;

    private static final PageTemplate binderPage = GWT.create(PageTemplate.class);

    public AbstractSearchPage() {
        this(true);
    }

    public AbstractSearchPage(boolean defaultDataTable) {
        page = new Page();
        DataTable dataTable = null;
        if (defaultDataTable) {
            dataTable = new DataTable(getSearchServiceAsync(), getCRUDServiceAsync(), getQueryDefinitionDTO(), getModificationPage(), getSearchPageConfigEnum()) {
                @Override
                protected void removeSuccessCustomAction() {
                    dataTableRemoveCustomAction();
                }
            };
        } else {
            dataTable = getCustomDataTable();
        }

        HTMLPanel pagePanel = binderPage.createAndBindUi(page);

        page.setProperties(getSearchPageConfigEnum());
        page.addToPageContent(getContentPanel());
        getSearchPanel().add(getSearchForm(dataTable));

        initWidget(pagePanel);
        dataTable.show(getResultsPanel());
    }

    protected void dataTableRemoveCustomAction() {
        //To be override
    }

    protected DataTable getCustomDataTable() {
        return null;
    }


    protected QueryDefinitionDTO getQueryDefinitionDTO() {
        return new QueryDefinitionDTO();
    }

    protected abstract PageConfigSearchEnum getSearchPageConfigEnum();

    protected abstract PageEnum getModificationPage();

    //services
    protected abstract SearchServiceAsync getSearchServiceAsync();

    protected abstract CRUDServiceAsync getCRUDServiceAsync();

    protected abstract SuggestServiceAsync getSuggestServiceAsync();

    //panels
    protected abstract HTMLPanel getResultsPanel();

    protected abstract HTMLPanel getSearchPanel();

    protected abstract HTMLPanel getContentPanel();

    //search form
    protected abstract Composite getSearchForm(DataTable dataTable);

}
